/**
 * All rights Reserved
 *
 * @Title: AbstractMongoConvertService.java
 * @Package com.amigo.online.provider.sync.abs
 * @Description: (用一句话描述该文件做什么)
 * @author: zxy  QQ:1363320658
 * @date: 2018年12月18日 上午10:29:55
 * @version V1.0
 * @Copyright: 2018  All rights reserved.
 */
package com.amigo.online.provider.sync.abs;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.provider.common.ClassNameConvertEnum;

/**
 * @version: V 1.0.0
 * @Description: （对类进行功能描述） 
 * @author: zxy  QQ:1363320658
 * @date: 2018年12月18日 上午10:29:55  
 *
 */

public abstract class AbstractMongoConvertService {
    /**
     * 通用logger记录
     */
    protected final Logger logger = LoggerFactory.getLogger(getClass());

    public void CommonConvert(ColumnHandleMap handlerMap, Document doc, JSONObject source) {
        Map<String, String> projectMap = handlerMap.getProjectMap();
        Map<String, String> classMap = handlerMap.getClassMap();
        for (String temp : handlerMap.getColumnList()) {
            String projectColumn = projectMap.get(temp);
            String className = classMap.get(temp);
            if (StringUtils.isEmpty(projectColumn)) {
                projectColumn = temp;
            }
            if (className == null) {
                doc.append(projectColumn, source.get(temp));
            } else {
                try {
                    Class<?> clazz = Class.forName(ClassNameConvertEnum.getColumnName(className));
                    doc.append(projectColumn, source.getObject(temp, clazz));
                } catch (ClassNotFoundException e) {
                    logger.info("更新获取中" + temp + "字段类型失败！");
                }
            }
        }
        CommonCoordinatesHandle(source, doc);
    }

    public void CommonCoordinatesHandle(JSONObject source, Document doc) {
        List<Double> point = new ArrayList<Double>();
        String longitude = source.getString("lng");
        String latitude = source.getString("lat");
        if (longitude == null) {
            longitude = source.getString("longitude");
        }
        if (latitude == null) {
            latitude = source.getString("latitude");
        }
        if (longitude != null) {
            try {
                point.add(Double.parseDouble(longitude));
            } catch (Exception e) {
                point.add(0d);
                logger.error("经纬度错误longitude:{}", longitude);
            }
        } else {
            point.add(0d);
        }
        if (latitude != null) {
            try {
                point.add(Double.parseDouble(latitude));
            } catch (Exception e) {
                point.add(0d);
                logger.error("经纬度错误latitude:{}", latitude);
            }
        } else {
            point.add(0d);
        }
        Document loc = new Document();
        loc.append("type", "Point").append("coordinates", point);
        doc.append("loc", loc);
    }
}
